package protocol.JT905_2014;

import protocol.SZ_2021.SZ_2021Utils;

import java.util.HashMap;

public class Handle905_2014JT0B implements IHandle905_2014JT {
    @Override
    public String handle(JT_905_2014Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x01:
                return handle0B01(cmd);
            case 0x03:
                return handle0B03(cmd);
            case 0x04:
                return handle0B04(cmd);
            case 0x05:
                return handle0B05(cmd);
            case 0x07:
                return handle0B07(cmd);
            case 0x08:
                return handle0B08(cmd);
            case 0x10:
                return handle0B10(cmd);
            case 0x11:
                return handle0B11(cmd);
            default:
                return "905:未匹配的命令字"+(cmd.readOrder()+","+cmd.readSubOrder());
        }
    }

    private String handle0B11(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:设备巡检应答");
        builder.append("\r\n");

        do{
            builder.append("终端设备序列号:"+cmd.readBCD(5));
            builder.append("\r\n");
            builder.append("硬件版本号:"+cmd.readBCD(1));
            builder.append("\r\n");
            builder.append("软件版本号:"+cmd.readBCD(2));
            builder.append("\r\n");
            builder.append("终端设备状态:"+cmd.readInt(4));
            builder.append("\r\n");
            builder.append("终端报警标志:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("签到缓存数据条数:"+cmd.readInt());
            builder.append("\r\n");
            builder.append("签退缓存数据条数:"+cmd.readInt());
            builder.append("\r\n");
            builder.append("营运记录缓存条数:"+cmd.readInt());
            builder.append("\r\n");
            builder.append("一卡通交易缓存条数:"+cmd.readInt());
            builder.append("\r\n");
            builder.append("-----------------");
            builder.append("\r\n");
        }while (cmd.leftSize() > 10);

        return builder.toString();
    }

    private String handle0B10(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:外围设备指令上行透传");
        builder.append("\r\n");

        int dev = cmd.readInt(1);
        builder.append("设备类型:"+JT905_2014Utils.toDeviceType(dev));
        builder.append("\r\n");

        builder.append("厂商标识:"+cmd.readInt(1));
        builder.append("\r\n");

        builder.append("命令类型:"+cmd.readBCD(2));
        builder.append("\r\n");

        builder.append("数据包:"+cmd.readBCD(cmd.leftSize()));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle0B05(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:运营数据上传");
        builder.append("\r\n");

        builder.append("空转重位置信息：");
        builder.append("\r\n");
        JT905_2014Utils.handle0200Base(cmd,builder);

        builder.append("重转空位置信息：");
        builder.append("\r\n");
        JT905_2014Utils.handle0200Base(cmd,builder);

        builder.append("营运ID："+cmd.readInt(4));
        builder.append("\r\n");
        builder.append("评价ID："+cmd.readInt(4));
        builder.append("\r\n");

        int type = cmd.readInt(1);
        HashMap<Integer,String> list = new HashMap<>();
        list.put(0x00,"没有做出评价");
        list.put(0x01,"满意");
        list.put(0x02,"一般");
        list.put(0x03,"不满意");
        list.put(0x04,"投诉");
        builder.append("评价选项("+type+"):"+list.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        builder.append("评价选项扩展:"+cmd.readBCD(2));
        builder.append("\r\n");

        builder.append("电召订单ID:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("计价器营运数据:"+cmd.readBCD(cmd.leftSize()));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle0B04(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:下班签退信息上传");
        builder.append("\r\n");

        JT905_2014Utils.handle0200Base(cmd,builder);

        builder.append("企业经营许可证号:"+cmd.readString(16));
        builder.append("\r\n");

        builder.append("从业资格证号:"+cmd.readString(19));
        builder.append("\r\n");

        builder.append("车牌号:"+cmd.readString(6));
        builder.append("\r\n");

        builder.append("K值:"+cmd.readBCD(2));
        builder.append("\r\n");

        builder.append("当班开机时间:"+cmd.readBCD(6));
        builder.append("\r\n");
        builder.append("当班关机时间:"+cmd.readBCD(6));
        builder.append("\r\n");
        builder.append("当班里程(xx.x公里):"+cmd.readBCD(3));
        builder.append("\r\n");
        builder.append("当班营运里程(xx.x公里):"+cmd.readBCD(3));
        builder.append("\r\n");
        builder.append("当班车次:"+cmd.readBCD(2));
        builder.append("\r\n");
        builder.append("计时时间:"+cmd.readBCD(3));
        builder.append("\r\n");
        builder.append("总计金额（格式XXXXX.X元）:"+cmd.readBCD(3));
        builder.append("\r\n");
        builder.append("卡收金额（格式XXXXX.X元）:"+cmd.readBCD(3));
        builder.append("\r\n");
        builder.append("卡次:"+cmd.readBCD(2));
        builder.append("\r\n");

        builder.append("班间公里(xx.x公里):"+cmd.readBCD(2));
        builder.append("\r\n");
        builder.append("总计公里(xx.x公里):"+cmd.readBCD(4));
        builder.append("\r\n");
        builder.append("总营运公里(xx.x公里):"+cmd.readBCD(4));
        builder.append("\r\n");
        builder.append("单价(格式XX.XX元):"+cmd.readBCD(2));
        builder.append("\r\n");
        builder.append("总营运次数:"+cmd.readInt(4));
        builder.append("\r\n");

        int type = cmd.readInt(1);
        HashMap<Integer,String> list = new HashMap<>();
        list.put(0x00,"正常签退");
        list.put(0x01,"强制签退");
        builder.append("签退类型("+type+"):"+list.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        builder.append("拓展信息:"+cmd.readBCD(cmd.leftSize()));
        builder.append("\r\n");
        return builder.toString();
    }

    private String handle0B03(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:上班签到信息上传");
        builder.append("\r\n");

        JT905_2014Utils.handle0200Base(cmd,builder);

        builder.append("经营许可证号:"+cmd.readString(16));
        builder.append("\r\n");

        builder.append("从业资格证号:"+cmd.readString(19));
        builder.append("\r\n");

        builder.append("车牌号:"+cmd.readString(6));
        builder.append("\r\n");

        builder.append("开机时间:"+cmd.readBCD(6));
        builder.append("\r\n");

        builder.append("拓展信息:"+cmd.readBCD(cmd.leftSize()));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle0B08(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:驾驶员取消订单");
        builder.append("\r\n");
        builder.append("业务ID:"+cmd.readInt(4));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle0B07(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:驾驶员电召任务完成确认");
        builder.append("\r\n");
        builder.append("业务ID:"+cmd.readInt(4));
        builder.append("\r\n");
        return builder.toString();
    }

    private String handle0B01(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:驾驶员订单抢答");
        builder.append("\r\n");
        builder.append("业务ID:"+cmd.readInt(4));
        builder.append("\r\n");
        return builder.toString();
    }
}
